package com.example.musiccommunity.ManagerPage;

import android.os.Bundle;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.example.musiccommunity.R;
import com.example.musiccommunity.util.OnDataRefreshListener;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

public class ManagerHome extends AppCompatActivity {

    private ArrayList<Fragment> fragments = new ArrayList<>();
    List<String> tabStrings = new ArrayList<>();
    private ViewPager viewPager;
    private TabLayout tabLayout;
    private Fragment_m_News fragment_m_news;
    private Fragment_m_notice fragment_m_notice;
    private Fragment_m_user fragment_m_user;
    private Fragment_m_Remark fragment_m_remark;


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_manager_home);

        fragment_m_news = new Fragment_m_News();
        fragment_m_notice = new Fragment_m_notice();
        fragment_m_user= new Fragment_m_user();
        fragment_m_remark= new Fragment_m_Remark();
        viewPager = (ViewPager) findViewById(R.id.view_pager_user);
        tabLayout = (TabLayout) findViewById(R.id.tl_title_user);

        fragments.add(fragment_m_news);
        fragments.add(fragment_m_notice);
        fragments.add(fragment_m_user);
        fragments.add(fragment_m_remark);

        tabStrings.add("资讯管理");
        tabStrings.add("公告管理");
        tabStrings.add("用户管理");
        tabStrings.add("评论管理");
        if (viewPager != null) {
            viewPager.setAdapter(new FragmentStatePagerAdapter(getSupportFragmentManager()) {
                @Override
                public Fragment getItem(int position) {
                    return fragments.get(position);
                }

                @Override
                public int getCount() {
                    return fragments.size();
                }

                @Override
                public void destroyItem(ViewGroup container, int position, Object object) {
                    super.destroyItem(container, position, object);
                }



                @Nullable
                @Override
                public CharSequence getPageTitle(int position) {
                    return tabStrings.get(position);
                }
            });
            viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                    // 不需要在这里做什么
                }

                @Override
                public void onPageSelected(int position) {
                    Fragment selectedFragment = fragments.get(position);
                    if (selectedFragment instanceof OnDataRefreshListener) {
                        System.out.println("刷新");
                        ((OnDataRefreshListener) selectedFragment).refreshData();
                    }
                }

                @Override
                public void onPageScrollStateChanged(int state) {
                    // 不需要在这里做什么
                }
            });
        } else {
            throw new NullPointerException();
        }

        tabLayout.setupWithViewPager(viewPager);
        viewPager.setOffscreenPageLimit(4);
    }
}